package com.springboot.demo.common.enums;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ranj on 2017/7/19 0019.
 *
 * 返回结果集
 */
public enum Status {

    SUCCESS(0, "执行成功"),

    FAIL(400, "执行失败"),

    PROCESSING(200, "处理中"),

    PARAMETER_NOT_EXIST(300, "参数不存在"),

    /** 非法的请求参数 */
    ILLEGAL_REQUEST_PARAMETER(301, "非法的请求参数"),

    SYSTEM_EXCEPTION(500, "系统异常");

    private final Integer code;

    private final String message;

    /**
     *
     */
    private Status(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * @return Returns the code.
     */
    public Integer getCode() {
        return code;
    }

    /**
     * @return Returns the message.
     */
    public String getMessage() {
        return message;
    }

    /**
     * @return Returns the code.
     */
    public Integer code() {
        return code;
    }

    /**
     * @return Returns the message.
     */
    public String message() {
        return message;
    }

    /**
     * @param code
     * @return Status
     */
    public static Status getByCode(Integer code) {
        for (Status _enum : values()) {
            if (_enum.getCode() == code) {
                return _enum;
            }
        }
        return null;
    }

    /**
     * @return
     */
    public static Integer getCode(Status resultEnum) {
        if (resultEnum == null) {
            return null;
        }
        return resultEnum.getCode();
    }

    /**
     * @return
     */
    public static String getMsgByCode(Integer code) {
        if (code == null) {
            return null;
        }
        Status _enum = getByCode(code);
        if (_enum == null) {
            return null;
        }
        return _enum.getMessage();
    }

    /**
     * @return List<Status>
     */
    public static List<Status> getAllEnum() {
        List<Status> list = new ArrayList<Status>(values().length);
        for (Status _enum : values()) {
            list.add(_enum);
        }
        return list;
    }

    /**
     * @return List<String>
     */
    public static List<Integer> getAllEnumCode() {
        List<Integer> list = new ArrayList<Integer>(values().length);
        for (Status _enum : values()) {
            list.add(_enum.code());
        }
        return list;
    }

}
